package cn.hongwei.mall.pms.service.impl;

import cn.hongwei.mall.pms.entity.AttrEntity;
import cn.hongwei.mall.pms.mapper.AttrMapper;
import cn.hongwei.mall.pms.vo.GroupVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hongwei.mall.common.bean.PageResultVo;
import cn.hongwei.mall.common.bean.PageParamVo;

import cn.hongwei.mall.pms.mapper.AttrGroupMapper;
import cn.hongwei.mall.pms.entity.AttrGroupEntity;
import cn.hongwei.mall.pms.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupMapper, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrMapper attrMapper;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<AttrGroupEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public List<GroupVo> queryByCategoryId(Long categoryId) {
        LambdaQueryWrapper<AttrGroupEntity> attrGroupEntityQueryWrapper = new LambdaQueryWrapper<>();
        attrGroupEntityQueryWrapper.eq(AttrGroupEntity::getCategoryId,categoryId);
        List<AttrGroupEntity> attrGroupEntities = this.baseMapper.selectList(attrGroupEntityQueryWrapper);
        return attrGroupEntities.stream().map(attrGroupEntity -> {
            GroupVo groupVo = new GroupVo();
            BeanUtils.copyProperties(attrGroupEntity,groupVo);
            //通过属性分组的groupID关联查询attr表
            List<AttrEntity> attrEntities = attrMapper.selectList(new LambdaQueryWrapper<AttrEntity>()
                    .eq(AttrEntity::getGroupId, attrGroupEntity.getId())
                    .eq(AttrEntity::getType, 1));//基本属性
            groupVo.setAttrEntities(attrEntities);
            return groupVo;
        }).collect(Collectors.toList());
    }

}